<script setup lang="ts">
import type { Message, Prescription } from '@/types/room'
import { IllnessTime, MsgType, PrescriptionStatus } from '@/enums'
import { timeOptions, flagOptions } from '@/services/constant/consult'
import { showImagePreview, showToast } from 'vant'
import 'vant/es/image-preview/style'
import 'vant/es/toast/style'
import type { Image } from '@/types/consult'
import { useUserStore } from '@/stores'
import { nextTick } from 'vue'
import dayjs from 'dayjs'
import EvaluateCard from '@/views/Room/components/EvaluateCard.vue'
import router from '@/router'
const store = useUserStore()
defineProps<{
  list: Message[]
}>()

// 处理患病时间
const getIllnessTime = (time?: IllnessTime) => {
  return timeOptions.find(item => item.value === time)?.label
}
// 处理是否去过医院
const getConsultRecordText = (flag?: 0 | 1) => {
  return flagOptions.find(item => item.value == flag)?.label
}
// 点击查看图片实现图片预览
const preview = (pictures?: Image[]) => {
  if (pictures && pictures.length) {
    showImagePreview(pictures.map(item => item.url))
  }
}
// 处理时间
const formatTime = (time?: string) => {
  if (time) {
    return dayjs(time).format('HH:mm:ss')
  }
}
// 监听图片加载完成事件
const onLoad = async () => {
  await nextTick()
  // 让滚动条滚动到body的最大可视区高度
  window.scrollTo(0, document.body.scrollHeight)
}

// 获取原始处方图片
/* const showPrescription = async (id?: string) => {
  if (id) {
    const res = await getPrescriptionPic(id)
    // 实现图片预览
    showImagePreview([res.data.url])
  }
} */
import { useShowPrescription } from '@/composable'
const { showPrescription } = useShowPrescription()


// 购买药品
const buy = (prescription?: Prescription) => {
  if (prescription) {
    // 如果处方失效：提示即可
    if (prescription.status === PrescriptionStatus.Invalid) return showToast('处方已失效')
    // 如果没付款且有药品订单ID，代表已经生成订单没付款：去订单列表查看所有的药品订单
    if (prescription.status === PrescriptionStatus.NotPayment && prescription.orderId) return router.push('/order')
    // 如果没药品订单ID：去预支付页面生成的订单
    if (!prescription.orderId) return router.push(`/order/pay?id=${prescription.id}`)
    // 有订单并付款了，那么就去药品订单详情查看该药品订单信息
    router.push(`/order/${prescription.orderId}`)
  }

}

</script>

<template>
  <!-- 占位标签，不占dom空间，但是可以在上面循环和分支判断 -->
  <template v-for="{ id, msgType, msg, createTime, from, to, fromAvatar, toAvatar } in list" :key="id">
    <!-- 病情描述消息 -->
    <div class="msg msg-illness" v-if="msgType === MsgType.CardPat">
      <div class="patient van-hairline--bottom" v-if="msg.consultRecord">
        <p>{{ msg.consultRecord.patientInfo.name }} {{ msg.consultRecord.patientInfo.genderValue }} {{
          msg.consultRecord.patientInfo.age }}岁</p>
        <p>{{ getIllnessTime(msg.consultRecord.illnessTime) }} | {{
          getConsultRecordText(msg.consultRecord.consultFlag) }}</p>
      </div>
      <van-row v-if="msg.consultRecord">
        <van-col span="6">病情描述</van-col>
        <van-col span="18">{{ msg.consultRecord.illnessDesc }}</van-col>
        <van-col span="6">图片</van-col>
        <van-col span="18" @click="preview(msg.consultRecord.pictures)">点击查看</van-col>
      </van-row>
    </div>

    <!-- 温馨提示-通知 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.NotifyTip">
      <div class="content">
        <span class="green">温馨提示：</span>
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 普通通知 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.Notify">
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 发送文字 -->
    <div class="msg msg-to" v-if="msgType === MsgType.MsgText && store.user?.id === from">
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
      <van-image :src="fromAvatar" />
    </div>
    <!-- 接收文字 -->
    <div class="msg msg-from" v-if="msgType === MsgType.MsgText && store.user?.id !== from">
      <van-image :src="toAvatar" />
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
    </div>
    <!-- 发送图片 -->
    <div class="msg msg-to" v-if="msgType === MsgType.MsgImage && store.user?.id === from">
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <van-image show-loading @load="onLoad" fit="contain" :src="msg.picture?.url" />
      </div>
      <van-image :src="fromAvatar" />
    </div>

    <!-- 接收图片 -->
    <div class="msg msg-from" v-if="msgType === MsgType.MsgImage && store.user?.id !== from">
      <van-image :src="toAvatar" />
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <van-image show-loading @load="onLoad" fit="contain" :src="msg.picture?.url" />
      </div>
    </div>
    <!-- 处方消息 -->
    <div class="msg msg-recipe" v-if="msgType === MsgType.CardPre && store.user?.id === to">
      <div class="content">
        <div class="head van-hairline--bottom">
          <div class="head-tit">
            <h3>电子处方</h3>
            <p @click="showPrescription(msg.prescription?.id)">原始处方 <van-icon name="arrow"></van-icon></p>
          </div>
          <p>{{ msg.prescription?.name }} {{ msg.prescription?.genderValue }} {{ msg.prescription?.age }}岁 {{
            msg.prescription?.diagnosis }}</p>
          <p>开方时间：{{ formatTime(msg.prescription?.createTime) }}</p>
        </div>
        <div class="body">
          <div class="body-item" v-for="med in msg.prescription?.medicines" :key="med.id">
            <div class="durg">
              <p>{{ med.name }} {{ med.specs }}</p>
              <p>{{ med.usageDosag }}</p>
            </div>
            <div class="num">x{{ med.quantity }}</div>
          </div>
        </div>
        <div class="foot"><span @click="buy(msg.prescription)">购买药品</span></div>
      </div>
    </div>
    <!-- 评价信息 -->
    <div class="msg msg-comment" v-if="msgType === MsgType.CardEva || msgType === MsgType.CardEvaForm">
      <evaluate-card :evaluateDoc="msg.evaluateDoc"></evaluate-card>
    </div>

    <!-- 订单取消|结束问诊 -->
    <div class="msg msg-tip msg-tip-cancel" v-if="msgType === MsgType.NotifyCancel">
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
  </template>
</template>

<style lang="scss" scoped>
@import './room.scss';
</style>